package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.config.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.config.converter.StringToItemTypeConverter;
import com.atguigu.lease.web.admin.interceptor.AuthenticationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private StringToItemTypeConverter stringToItemTypeConverter;
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;
    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    @Override
    public void addFormatters(FormatterRegistry registry) {
        log.info("自定义枚举转换机制已开启");
        registry.addConverterFactory(this.stringToBaseEnumConverterFactory);
    }

    //这种方法只能修改一个枚举类的转换
//    @Override
//    public void addFormatters(FormatterRegistry registry) {
//        registry.addConverter(this.stringToItemTypeConverter);
//    }

    public void addInterceptors(InterceptorRegistry registry){
        log.info("拦截器已开启");
        registry.addInterceptor(this.authenticationInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login/**");
    }

}
